package edu.chinasoft.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.User;
import edu.chinasoft.service.UserService;
import edu.chinasoft.service.UserServiceImp;

/**
 * 
 * @author cjogng
 * @date 2020年1月17日:上午10:53:32
 * @version V1.0
 * @Description: 添加用户
*/
@WebServlet(name="myaddUserforServlet",value="/servlet/adduserforservlet")
public class  AddUserforServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger( AddUserforServlet .class);
	private UserService userService=new UserServiceImp();
	
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddUserforServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//获取表单提交的数据
		String user_name = request.getParameter("user_name");
		String user_password = request.getParameter("user_password");
		String user_desc = request.getParameter("user_desc");
		String user_flag = request.getParameter("user_flag");
		
		//把数据封装到User，然后调用服务器层插入到数据库
		
		User result=new User(user_name,user_password,user_desc,user_flag);
		
		User userReturn = userService.addUser(result);
		
		//如果成功跳转到添加用户成功页面
		
		
		//如果失败跳转到失败页面
		
		if(userReturn!=null) {
			response.sendRedirect(""+request.getContextPath()+"/servlet/finduserpageforservlet");
		}else {
			response.sendRedirect(""+request.getContextPath()+"/addusererror.html");
		}
		
		
		
		
		logger.debug(" AddUserforServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddUserforServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" AddUserforServlet >doPost end.......");
	}
	
}